<link rel="stylesheet" type="text/css" media="screen" href="css/codegen.css" />
<div class="navBc">Home > Template > 코드생성기2</div>
	<div id="master">
		<div id="master1">
			<label for="systemGubun">시스템구분</label>
			<select id="systemGubun">
				<option value="com">공통</option>
				<option value="2">학사</option>
				<option value="3">행정</option>
				<option value="4">연구</option>
				<option value="5">부속</option>
			</select>
			<label for="unitUpmuGubun">단위업무구분</label><input type="text" id="unitUpmuGubun" value="comm"/>
			<label for="smallUnitUpmuGubun">소단위업무구분</label><input type="text" id="smallUnitUpmuGubun" value="tmpl"/>
		</div>
		<div id="master2">
			<label for="largeClassUpmuName">대분류업무명</label><input type="text" id="largeClassUpmuName" value="템플릿"/><button id="btnAction">Action생성</button>
			<label for="upmuName">업무명</label><input type="text" id="upmuName" value="코드_생성_기"/><button id="btnEtc">기타생성</button>
			<label for="author">작성자</label><input type="text" id="author" value="홍길동"/>
		</div>
		<div id="master3">
			<label for="actionclassname">Action클래스명</label><input type="text" id="actionclassname" />
			<label for="serviceclassname">서비스클래스명</label><input type="text" id="serviceclassname" />
			<label for="modelclassname">모델클래스명</label><input type="text" id="modelclassname" />
			<label for="sqlclassname">SQL클래스명</label><input type="text" id="sqlclassname" />
		</div>
		<div id="master4">
			<label for="miplatform">Miplatform</label><input type="text" id="miplatform" />
			<label for="jqueryform">jQuery Form</label><input type="text" id="jqueryform" />
		</div>
	</div>
	<div>
			<label for="">오라클 User 테이블 명(모델생성시 필요)</label>
			<label for="schemaname">스키마명</label>
			<select id="schemaname">
				<option value="1">공통</option>
				<option value="2">학사</option>
				<option value="3">행정</option>
				<option value="4">연구</option>
				<option value="5">부속</option>
			</select>
			<label for="">COM 계정에 Grant를 줘야 조회가 됩니다</label>
			<button id="query">조회</button>
	</div>
	<div>
		<div class="tableList"><table id="tableList"></table></div>
		<div class="columnInfo"><table id="columnInfo"></table></div>
	</div>

	<div id="tabs8">
		<ul>
			<li id="current"><a href="#"><span>Groovy</span></a></li>
			<li><a href="#"><span>Model</span></a></li>
			<li><a href="#"><span>Service</span></a></li>
			<li><a href="#"><span>Action</span></a></li>
			<li><a href="#"><span>Miplaform</span></a></li>
			<li><a href="#"><span>jQuery</span></a></li>
		</ul>
	</div>
	<div><button id="btnTemplate">Action템플릿조회</button><button id="btnFile">Action생성</button></div>
	<div><textarea id="srcTemplate" rows="20" cols="100"></textarea></div>
	<form action="#" onsubmit="return savefile(this);">
	<div><input id="submit" type="submit"/></div>
	</form>

<script type="text/javascript">
	var url;
	var unitUpmuGubun;
	var smallUnitUpmuGubun;
	var largeClassUpmuName;

	// Action생성 버튼 클릭
	$("#btnAction").click(function(){
		unitUpmuGubun = $("#unitUpmuGubun").val();
		smallUnitUpmuGubun = $("#smallUnitUpmuGubun").val();
		largeClassUpmuName = $("#largeClassUpmuName").val();

		if ($("#master4").find('.msg').length > 0){
			$("#master4").children().remove('.msg');
		}

		if (unitUpmuGubun==null || unitUpmuGubun.replace(/^\s*/,'') == "") {
			if ($("#master4").find('.msg').length == 0) {
				$("#master4").append('<label class="msg">단위업무구분을 입력해주세요!!!</label>');
			}
			return;
		}

		if (smallUnitUpmuGubun==null || smallUnitUpmuGubun.replace(/^\s*/,'') == "") {
			if ($("#master4").find('.msg').length == 0) {
				$("#master4").append('<label class="msg">소단위업무구분을 입력해주세요!!!</label>');
			}
			return;
		}

		if (largeClassUpmuName==null || largeClassUpmuName.replace(/^\s*/,'') == "") {
			if ($("#master4").find('.msg').length == 0) {
				$("#master4").append('<label class="msg">대분류업무명을 입력해주세요!!!</label>');
			}
			return;
		}

		if ($("#master4").find('.msg').length > 0){
			$("#master4").children().remove('.msg');
		}

		url='http://localhost:8080/com/com/comm/tmpl/getActionNm.action?unitUpmuNm='+$("#smallUnitUpmuGubun").val();
		//url='actionbutton.json';
		$.ajax({
			type: "get",
			url: url,
			datatype: "json",
			contentType: "application/extJs+sua; charset=utf-8",
			success: function(json){
				$("#master4").append('<label class="msg">OK!!!</label>');
				if(json.DS_ACTION == null) {
					alert("단위업무명이 존재하지 않습니다");
				} else {
					$.each(json.DS_ACTION,function(i,data){
						$("#actionclassname").val(data.actionClass);
					});
				}
			},
			error: function(data, status, err) {
				console.log("error forward : "+data);
				alert("통신 오류입니다. 시스템관리자에게 문의하시기 바랍니다.");
			}
		});
	});

	// 기타생성 버튼 클릭
	$("#btnEtc").click(function(){
		unitUpmuGubun = $("#unitUpmuGubun").val();
		smallUnitUpmuGubun = $("#smallUnitUpmuGubun").val();
		largeClassUpmuName = $("#largeClassUpmuName").val();

		if ($("#master4").find('.msg').length > 0){
			$("#master4").children().remove('.msg');
		}

		if (unitUpmuGubun==null || unitUpmuGubun.replace(/^\s*/,'') == "") {
			if ($("#master4").find('.msg').length == 0) {
				$("#master4").append('<label class="msg">단위업무구분을 입력해주세요!!!</label>');
			}
			return;
		}

		if (smallUnitUpmuGubun==null || smallUnitUpmuGubun.replace(/^\s*/,'') == "") {
			if ($("#master4").find('.msg').length == 0) {
				$("#master4").append('<label class="msg">소단위업무구분을 입력해주세요!!!</label>');
			}
			return;
		}

		if (largeClassUpmuName==null || largeClassUpmuName.replace(/^\s*/,'') == "") {
			if ($("#master4").find('.msg').length == 0) {
				$("#master4").append('<label class="msg">대분류업무명을 입력해주세요!!!</label>');
			}
			return;
		}

		if ($("#master4").find('.msg').length > 0){
			$("#master4").children().remove('.msg');
		}

		url='http://localhost:8080/com/com/comm/tmpl/getEtcClassNm.action?upmuNm='+$("#upmuName").val();
		//url=encodeURIComponent(url1);
		//url='actionbutton.json';
		$.ajax({
			type: "get",
			url: url,
			datatype: "json",
			contentType: "application/extJs+sua; charset=utf-8",
			success: function(json){
				$("#master4").append('<label class="msg">OK!!!</label>');
				if(json.DS_CLASS == null) {
					alert("단위업무명이 존재하지 않습니다");
				} else {
					console.log(json.DS_CLASS.length);
					console.log(json.DS_CLASS[0].class);
					$("#modelclassname").val(json.DS_CLASS[0].class);
					$("#serviceclassname").val(json.DS_CLASS[0].class+"Svc");
					$("#sqlclassname").val(json.DS_CLASS[0].class+"Qry");
					$("#miplatform").val(json.DS_CLASS[0].class);
					$("#jqueryform").val(json.DS_CLASS[0].class);
					//$.each(json.DS_CLASS,function(i,data){
					//	console.log(data.class);
					//});
				}
			},
			error: function(data, status, err) {
				console.log("error forward : "+data);
				alert("통신 오류입니다. 시스템관리자에게 문의하시기 바랍니다.");
			}
		});
	});

	// 조회버튼 클릭
	$("#query").click(function(){
		$("#tableList").html("");
		$("#columnInfo").html("");

		$("#tableList").append(
					'<tr>'+
						'<th class="no">순번</th>'+
						'<th class="tablename">테이블명</th>'+
						'<th class="remarks">테이블주석</th>'+
					'</tr>'
		);

		url='http://localhost:8080/com/com/comm/tmpl/getTableJeongbo.action?skima=COM';
		//url='tablelist.json';
		$.ajax({
			type: "get",
			url: url,
			datatype: "json",
			contentType: "application/extJs+sua; charset=utf-8",
			success: function(json){
				if(json.DS_TMPL010 == null) {
				   $("#tableList").append(
					   '<tr>'+
					   '<td colspan="8">검색된 결과가 없습니다.</td>'+
				   	'</tr>');
				} else {
					$.each(json.DS_TMPL010,function(i,data){
						$('<tr>').append($('<td>').addClass('no').html(i))
								 .append($('<td>').addClass('tablename').html(data.tableName))
								 .append($('<td>').addClass('remarks').html(data.comments))
								 .bind("click",function(){clickedTableList($(this));})
								 .appendTo("#tableList");
//						$("#tableList").append(
//							'<tr>'+
//								'<td class="no">'+i+'</td>'+
//								'<td class="tablename">'+data.tableName+'</td>'+
//								'<td class="remarks">'+data.comments+'</td>'+
//							'</tr>');
					});

				}
			},
			error: function(data, status, err) {
				console.log("error forward : "+data);
				alert("통신 오류입니다. 시스템관리자에게 문의하시기 바랍니다.");
			}
		});
	});

	// 테이블 라인 클릭
	function clickedTableList($obj) {
		var tablename = $obj.find("td.tablename").text();
 		$("#columnInfo").html("");
		$("#columnInfo").append(
					'<tr>'+
						'<th class="chk"><input class="chk" type="checkbox"/></th>'+
						'<th class="columnName">컬럼ID</th>'+
						'<th class="tablename">테이블명</th>'+
						'<th class="comments">컬럼주석</th>'+
						'<th class="dataLength">Length</th>'+
						'<th class="constraintType">PK</th>'+
						'<th class="dataDefault">Data Default</th>'+
					'</tr>'
		);
		url='http://localhost:8080/com/com/comm/tmpl/getColumnJeongbo.action?skima=COM&tableNm='+tablename;
		//url='columninfo.json';
		//chk=0, columnId=1, owner=COM, tableName=PROGMA01TT, columnName=PROGRAM_ID,
		//comments=프로그램ID, dataLength=8, constraintType=P, dataDefault=null
		$.ajax({
			type: "get",
			url: url,
			datatype: "json",
			contentType: "application/extJs+sua; charset=utf-8",
			success: function(json){
				if(json.DS_TMPL011 == null) {
				   $("#columnInfo").append(
					   '<tr>'+
					   '<td colspan="8">검색된 결과가 없습니다.</td>'+
				   	'</tr>');
				} else {
					$.each(json.DS_TMPL011,function(i,data){
						$("#columnInfo").append(
							'<tr>'+
								'<td class="chk"><input class="chk" type="checkbox"/></td>'+
								'<td class="columnId">'+data.columnName+'</td>'+
								'<td class="tablename">'+data.tableName+'</td>'+
								'<td class="comments">'+data.comments+'</td>'+
								'<td class="dataLength">'+data.dataLength+'</td>'+
								'<td class="constraintType">'+data.constraintType+'</td>'+
								'<td class="dataDefault">'+data.dataDefault+'</td>'+
							'</tr>');
					});
				}
			},
			error: function(data, status, err) {
				console.log("error forward : "+data);
				alert("통신 오류입니다. 시스템관리자에게 문의하시기 바랍니다.");
			}

		});

/*
		$("#columnInfo").jqGrid({
			url: url,
			datatype: "json",
			contentType: "application/extJs+sua; charset=utf-8",
			colNames:['chk','컬럼ID','테이블명', '컬럼주석','Length','PK','Data Default'],
			colModel:[
				{name:'chk',index:'chk'},
				{name:'columnId',index:'columnId'},
				{name:'tableName',index:'tableName', align:"left"},
				{name:'comments',index:'comments', align:"left"},
				{name:'dataLength', index:'dataLength', align:"center"},
				{name:'constraintType',index:'constraintType', align:"center"},
				{name:'dataDefault',index:'dataDefault', sortable:false}
			],
			sortname: 'id',
			viewrecords: true,
			sortorder: "desc"
		});
*/
	};

	// Action 템플릿조회 버튼 클릭
	$("#btnTemplate").click(function(){
		var datafile = "templates/Action";
		var objXml;

		if (!(document.getElementById)) {
			alert("시스템 오류입니다.");
			return;
		}
		objXml = new XMLHttpRequest();
		objXml.open("GET",datafile,false);
		objXml.send(null);
		var $src = objXml.responseText;

		var strPackage1, strPackage2, strPackage3, strUnitUpmuNm, strAuthor, strYear
		var strMM, strDD, strActionClass, strUpmuComent, strServiceClass, strUpmuComent
		var strService, strModelClass, strModel, strDS

		strPackage1 = $("#systemGubun").val();         //시스템구분
		strPackage2 = $("#unitUpmuGubun").val();       //단위업무구분
		strPackage3 = $("#smallUnitUpmuGubun").val();  //소단위업무구분
		strUnitUpmuNm = $("#largeClassUpmuName").val();  //업무명
		strAuthor = $("#author").val();  	//작성자
		var now = new Date();
		strYear = now.getFullYear();
		strMM = now.getMonth()+1;
		strDD = now.getDate();
		strActionClass = $("#actionclassname").val();  //Action클래스명
		strUpmuComent = $("#upmuName").val();  //업무명
		strServiceClass = $("#serviceclassname").val();  //Service클래스명
		strService = strServiceClass.charAt(0).toLowerCase() + strServiceClass.substring(1);
		strModelClass = $("#modelclassname").val();  //Model클래스명
		strModel = strModelClass.charAt(0).toLowerCase() + strModelClass.substring(1);
		strDS = "DS_" + strModel.toUpperCase();

		console.log("strPackage1: " + strPackage1);
		console.log("strPackage2: " + strPackage2);
		console.log("strPackage3: " + strPackage3);
		console.log("strUnitUpmuNm: " + strUnitUpmuNm);
		console.log("strAuthor: " + strAuthor);
		console.log("strYear: " + strYear);
		console.log("strMM: " + strMM);
		console.log("strDD: " + strDD);
		console.log("strActionClass: " + strActionClass);
		console.log("strUpmuComent: " + strUpmuComent);
		console.log("strServiceClass: " + strServiceClass);
		console.log("strService: " + strService);
		console.log("strModelClass: " + strModelClass);
		console.log("strModel: " + strModel);
		console.log("strDS: " + strDS);

		var $str="";
		for(var i=0;i<$src.length;i++) {
			if ($src.substring(i,i+3) == "$01") {
				$str=$str+strPackage1; i=i+2;
			} else if ($src.substring(i,i+3) == "$02") {
				$str=$str+strPackage2; i=i+2;
			} else if ($src.substring(i,i+3) == "$03") {
				$str=$str+strPackage3; i=i+2;
			} else if ($src.substring(i,i+3) == "$04") {
				$str=$str+strUnitUpmuNm; i=i+2;
			} else if ($src.substring(i,i+3) == "$05") {
				$str=$str+strAuthor; i=i+2;
			} else if ($src.substring(i,i+3) == "$06") {
				$str=$str+strYear; i=i+2;
			} else if ($src.substring(i,i+3) == "$07") {
				$str=$str+strMM; i=i+2;
			} else if ($src.substring(i,i+3) == "$08") {
				$str=$str+strDD; i=i+2;
			} else if ($src.substring(i,i+3) == "$09") {
				$str=$str+strActionClass; i=i+2;
			} else if ($src.substring(i,i+3) == "$10") {
				$str=$str+strUpmuComent; i=i+2;
			} else if ($src.substring(i,i+3) == "$11") {
				$str=$str+strServiceClass; i=i+2;
			} else if ($src.substring(i,i+3) == "$12") {
				$str=$str+strService; i=i+2;
			} else if ($src.substring(i,i+3) == "$13") {
				$str=$str+strModelClass; i=i+2;
			} else if ($src.substring(i,i+3) == "$14") {
				$str=$str+strModel; i=i+2;
			} else if ($src.substring(i,i+3) == "$15") {
				$str=$str+strDS; i=i+2;
			} else {
				$str=$str+$src.charAt(i);
			}
		};
		$("textarea#srcTemplate").val($str);
	});

	// Action 파일 생성
	$("#btnFile").click(function(){
		var $str = $("textarea#srcTemplate").val();

		var w = window.frames.w;
		if( !w ) {
			w = document.createElement( 'iframe' );
			w.id = 'w';
			w.style.display = 'none';
			document.body.insertBefore( w, null );
			w = window.frames.w;
			if( !w ) {
				w = window.open( '', '_temp', 'width=100,height=100' );
				if( !w ) {
					window.alert( 'Sorry, the file could not be created.' ); return false;
				}
			}
		}

		var d = w.document;
		var ext = '.txt';
		var name = 'xxxAction.java';
		//ext = f.ext.options[f.ext.selectedIndex],
		//name = f.filename.value.replace( /\//g, '\\' ) + ext;

		//d.charset = ext.value;
		d.open( 'text/plain', 'replace' );
		var $str = $("textarea#srcTemplate").val();
		if( ext ==='.txt' ) {
			d.write( $str );
			d.close();
		} else {  //  '.html'
			d.close();
			d.body.innerHTML = '\r\n' + $str + '\r\n';
		}

		if( d.execCommand( 'SaveAs', null, name ) ){
			window.alert( name + ' has been saved.' );
		} else {
			window.alert( 'The file has not been saved.\nIs there a problem?' );
		}
		w.close();
		return false;  //  don't submit the form
	});
/*
	function getNavigatorInfoStr()
	{
	    var name = navigator.appName, ver = navigator.appVersion,
	        ver_int = parseInt(navigator.appVersion), ua = navigator.userAgent, infostr;
	    if(name == "Microsoft Internet Explorer")
	    {
	        if(ver.indexOf("MSIE 3.0") != -1) return "Internet Explorer 3.0x";
	        if(ver_int != 4) return "Internet Explorer " + ver.substring(0, ver.indexOf(" "));

	        var real_ver = parseInt(ua.substring(ua.indexOf("MSIE ") + 5));
	        if(real_ver &gt;= 7) infostr = "Windows Internet Explorer ";
	        else infostr = "Microsoft Internet Explorer ";

	        if(ua.indexOf("MSIE 5.5") != -1) return infostr + "5.5";
	        else return infostr + real_ver + ".x";

	        return "Internet Explorer";
	    }
	    else if(name == "Netscape")
	    {
	        if(parseInt(ua.substring(8, 8)) &lt;= 4)
	          return "Netscape " + ver.substring(0, ver.indexOf(" "));
	        else if(ua.lastIndexOf(" ") &lt; ua.lastIndexOf("/"))
	          return ua.substring(ua.lastIndexOf(" "));
	        else return "Netscape";
	    }
	    else return name;
	}

	function userAgent()
	{
		var browserType = "";
		if(navigator.userAgent.indexOf("MSIE") != -1)
		{
			 browserType = "IE";
			 return browserType;
		}
		if(navigator.userAgent.indexOf("Firefox") != -1)
		{
			 browserType = "FF";
			 return browserType;
		}
		if(navigator.userAgent.indexOf("Mozilla") != -1)
		{
			 browserType = "MZ";
			 return browserType;
		}
		if(navigator.userAgent.indexOf("Opera") != -1)
		{
			 browserType = "OP";
			 return browserType;
		}
		if(navigator.userAgent.indexOf("Safari") != -1)
		{
			 browserType = "SF";
			 return browserType;
		}
		if(navigator.userAgent.indexOf("Mac") != -1)
		{
			 browserType = "MC";
			 return browserType;
		}
		browserType = "NG";
		return browserType;
	}
*/
</script>